#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/14 09:20
# @Author  : 王凯
# @File    : hunan_illegal.py
# @Project : spider-man
import datetime
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import parse_url_params


class HunanIllegalSpider(BaseTaxIllegalSpider):
    name: str = "hunan_illegal"
    province: str = "湖南"
    url: str = "https://hunan.chinatax.gov.cn/hardcase/20190408002097"  # 取首页

    def start_requests(self):
        url = "https://hunan.chinatax.gov.cn/hardcasegetdatanew"
        data = {
            "case_type": "1",
            "page": "1",
            "limit": "10",
            "is_search": "1",
            "taxpayerName": "",
            "taxpayerNumber": "",
            "organizationalCode": "",
            "place": "",
            "legalName": "",
            "legalIdCard": "",
            "financeName": "",
            "financeIdCard": "",
            "personName": "",
            "personIdCard": "",
            "_csrf": "",
        }
        yield self.FormRequest(url, method="POST", formdata=data, callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        yield from self.parse_detail(response, **kwargs)
        url = "https://hunan.chinatax.gov.cn/hardcasegetdatanew"
        root_url, params = parse_url_params(response.request.body.decode())
        total_pages = response.json().get("hardCasePage", {}).get("totalPages", 1)
        if total_pages and int(total_pages) >= 1:
            for i in range(2, int(total_pages) + 1):
                yield self.FormRequest(
                    url,
                    formdata={**params, "page": str(i), "_csrf": ""},
                    method="POST",
                    callback=self.parse_detail,
                )

    def parse_detail(self, response, **kwargs):
        data = response.json().get("data", [])
        for info in data:
            item = NetTaxIllegalProItem()
            mapping = {
                "taxpayerName": "company_name",
                "taxpayerNumber": "taxpayer_id",
                "organizationalCode": "org_code",
                "place": "address",
                "agency": "resp_intermediary",
            }

            for k, v in info.items():
                if mapping.get(k):
                    setattr(item, mapping.get(k), v)
                if info.get("legalName"):
                    item.legal_representative = "{},{},{}".format(
                        info.get("legalName"), "男" if info.get("legalSex") == 1 else "女", info.get("legalIdCard")
                    )
                if info.get("legalNameDuring"):
                    item.illegal_legal_representative = "{},{},{}".format(
                        info.get("legalNameDuring"),
                        "男" if info.get("legalSexDuring") == 1 else "女",
                        info.get("legalIdCardDuring"),
                    )

                if info.get("financeName"):
                    item.resp_financial = "{},{},{}".format(
                        info.get("financeName"),
                        "男" if info.get("financeSex") == 1 else "女",
                        info.get("financeIdCard"),
                    )
                if info.get("principalName"):
                    item.resp_person = "{},{},{}".format(
                        info.get("principalName"),
                        "男" if info.get("principalSex") == 1 else "女",
                        info.get("principalIdCard"),
                    )
                item.illegal_status = info.get("hardCaseType", {}).get("typeName")

            illegal_facts_src = info.get("content", "")
            illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", illegal_facts_src))
            illegal_facts = re.findall(
                r"(.*(?:主要存在|存在以下问题)(?:[^根]*?[。，;；])*)[^。]*?(?:根据|依照|依据)+", illegal_facts_src
            )
            illegal_facts = illegal_facts[0] if illegal_facts else ""
            if illegal_facts:
                basis_and_punishment = illegal_facts_src.replace(illegal_facts, "")
            else:
                illegal_facts = illegal_facts_src
                basis_and_punishment = ""
            item.basis_and_punishment = basis_and_punishment
            item.illegal_facts = illegal_facts

            item.province = self.province
            item.year = str(datetime.datetime.fromtimestamp(info["addTime"] / 1000).date()).replace("-", "")
            yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "hunan_illegal"])
